package com.myj.jim.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myj.jim.domain.Article;
import com.myj.jim.domain.Operator;
import com.myj.jim.domain.ResultMessage;
import com.myj.jim.domain.bootstraptable.BootstrapTable;
import com.myj.jim.domain.bootstraptable.BootstrapTableJSON;
import com.myj.jim.service.ArticleService;
import com.myj.jim.service.ArticleTypeService;
import com.myj.jim.service.SysLogService;
import com.myj.jim.util.BootstrapTableUtil;
import com.myj.jim.util.JimUtil;

@RequestMapping("/article")
@Controller
public class ArticleContoller {
	private static final String PAGE_AUTHORITY = "articleManage";
	
	private static final String NO_AUTHORITY_VIEW = "common/noAuthorityPage";
	
	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private ArticleTypeService articleTypeService;
	
	@Autowired
	private SysLogService sysLogService;
	
	@RequestMapping("/page")
    public String page(Model model, HttpSession session, HttpServletRequest req){
		String path = req.getContextPath();
		String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
		Operator op = (Operator)session.getAttribute("operator");
		if(op!=null){
			if(JimUtil.haveAuthority(op, PAGE_AUTHORITY)){
				model.addAttribute("articleTypeMap", JSONObject.fromObject(articleTypeService.getArticleTypeMap()));
				return "article/page";
			}else{
				return NO_AUTHORITY_VIEW;
			}
			
		}else{
			//到登录页面
			return "redirect:"+basePath+"index.jsp";
		}
    	
    }
	
	@RequestMapping("/list")
	@ResponseBody
	public BootstrapTableJSON<Article> list(BootstrapTable bt, HttpServletRequest req){
		String type = StringUtils.trimToEmpty(req.getParameter("type"));
		Map<String,Object> map = BootstrapTableUtil.getParamMap(bt);
		if(StringUtils.isNotBlank(type)){
			map.put("type", type);
		}
		
		return articleService.findPage(map);
	}
	
	@RequestMapping("/newArticle")
	public String newArticle(Model model,HttpServletRequest req,HttpSession session){
		
		
		model.addAttribute("articleTypeList", articleTypeService.selectAllArticleType());
		return "article/newArticle";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public ResultMessage save(Article article,HttpServletRequest req,HttpSession session){
		ResultMessage rm = new ResultMessage();
		Operator op = (Operator) session.getAttribute("operator");
		
		if(articleService.save(article)){
			
			JimUtil.insertLog(sysLogService, req, op, "30001", article.getTitle(), article.getType(), "");
			
			rm.setSuccess(true);
			rm.setMessage("保存成功");
		}else{
			rm.setSuccess(false);
			rm.setMessage("保存失败");
		}
		return rm;
	}
	@RequestMapping("/edit")
	public String edit(Model model, Integer id){
		model.addAttribute("article", articleService.getArticleById(id));
		model.addAttribute("articleTypeList", articleTypeService.selectAllArticleType());
		return "article/edit";
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ResultMessage update(Article article,HttpServletRequest req,HttpSession session){
		ResultMessage rm = new ResultMessage();
		Operator op = (Operator) session.getAttribute("operator");
		if(articleService.update(article)){
			JimUtil.insertLog(sysLogService, req, op, "30002", article.getTitle(), article.getType(), "");
			rm.setMessage("保存成功");
			rm.setSuccess(true);
		}else{
			rm.setMessage("保存失败");
			rm.setSuccess(false);
		}
		return rm;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public ResultMessage delete(Integer id,HttpServletRequest req,HttpSession session){
		ResultMessage rm = new ResultMessage();
		Operator op = (Operator) session.getAttribute("operator");
		Article a = articleService.getArticleById(id);
		if(articleService.delete(id)){
			JimUtil.insertLog(sysLogService, req, op, "30003", a.getTitle(), a.getType(), "");
			rm.setMessage("删除成功");
			rm.setSuccess(true);
		}else{
			rm.setMessage("删除失败");
			rm.setSuccess(false);
		}
		return rm;
	}
	
	
}
